package com.xdclass.mysql.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

/**
 * @author daniel
 */
@Intercepts(
        @Signature(method = "query",
                type = Executor.class, args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
)
public class QueryInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long startTime =  System.currentTimeMillis();
        System.out.println("拦截方法开始,SQL执行----");
        Object proceed = invocation.proceed();
        System.out.println("拦截方法结束，SQL执行时间"+(System.currentTimeMillis()-startTime)+"ms----");
        return proceed;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
